package com.dlc.shop.coupon.multishop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.common.util.PageParam;
import com.dlc.shop.coupon.common.model.CouponUser;
import com.dlc.shop.coupon.common.service.CouponUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * @author lgh on 2018/12/27.
 */
@RestController
@RequestMapping("/admin/couponUser")
@Tag(name = "商家端优惠券用户接口")
@AllArgsConstructor
public class CouponUserController {

    private final CouponUserService couponUserService;

    @GetMapping("/page")
    @Operation(summary = "分页获取优惠券用户信息")
    @PreAuthorize("@pms.hasPermission('admin:couponUser:page')")
    public ServerResponseEntity<IPage<CouponUser>> page(CouponUser couponUser, PageParam<CouponUser> page) {
        IPage<CouponUser> couponUsers = couponUserService.page(page, new LambdaQueryWrapper<CouponUser>());
        return ServerResponseEntity.success(couponUsers);
    }

    @GetMapping("/info/{id}")
    @Operation(summary = "根据优惠券用户id获取信息")
    @Parameter(name = "id", description = "优惠券用户id" )
    @PreAuthorize("@pms.hasPermission('admin:couponUser:info')")
    public ServerResponseEntity<CouponUser> info(@PathVariable("id") Long id) {
        CouponUser couponUser = couponUserService.getById(id);
        return ServerResponseEntity.success(couponUser);
    }

    @PostMapping
    @Operation(summary = "新增优惠券用户")
    @PreAuthorize("@pms.hasPermission('admin:couponUser:save')")
    public ServerResponseEntity<Void> save(@RequestBody @Valid CouponUser couponUser) {
        couponUserService.save(couponUser);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "修改优惠券用户")
    @PreAuthorize("@pms.hasPermission('admin:couponUser:update')")
    public ServerResponseEntity<Void> update(@RequestBody @Valid CouponUser couponUser) {
        couponUserService.updateById(couponUser);
        return ServerResponseEntity.success();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "根据优惠券用户id删除信息")
    @Parameter(name = "id", description = "优惠券用户id" )
    @PreAuthorize("@pms.hasPermission('admin:couponUser:delete')")
    public ServerResponseEntity<Void> delete(@PathVariable Long id) {
        couponUserService.removeById(id);
        return ServerResponseEntity.success();
    }
}
